package com.woniuxy.mapper.impl;

import com.woniuxy.mapper.StudentDao;
import com.woniuxy.entity.Student;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 作者
 * @create 2025-06-11 10:55
 * 通过IO操作实现学生CRUD
 */
public class StudentDaoImpl implements StudentDao {
    @Override
    public void insertStudent(Student student) {
        try (BufferedWriter bw = new BufferedWriter(
                new FileWriter("d:\\aaa\\students.txt", true));) {
            bw.write(student.getSid() + "-");
            bw.write(student.getSname() + "-");
            bw.write(student.getPwd() + "-");
            bw.write(student.getAge() + "-");
            bw.write(student.getEmail());
            bw.newLine();
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student selectBySname(String sname) {
        try (BufferedReader br = new BufferedReader(
                new FileReader("d:\\aaa\\students.txt"));) {
            Student student = null;
            String str = null;
            while ((str = br.readLine()) != null) {
                String[] strs = str.split("-");
                if (strs[1].equals(sname)) {
                    student = new Student();
                    student.setSid(strs[0]);
                    student.setSname(strs[1]);
                    student.setPwd(strs[2]);
                    student.setAge(Integer.parseInt(strs[3]));
                    student.setEmail(strs[4]);
                    break;
                }
            }
            return student;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student selectByEmail(String email) {
        try (BufferedReader br = new BufferedReader(
                new FileReader("d:\\aaa\\students.txt"));) {
            Student student = null;
            String str = null;
            while ((str = br.readLine()) != null) {
                String[] strs = str.split("-");
                if (strs[4].equals(email)) {
                    student = new Student();
                    student.setSid(strs[0]);
                    student.setSname(strs[1]);
                    student.setPwd(strs[2]);
                    student.setAge(Integer.parseInt(strs[3]));
                    student.setEmail(strs[4]);
                    break;
                }
            }
            return student;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public String selectMaxSid() {
        String maxSid = null;
        try (BufferedReader br = new BufferedReader(
                new FileReader("d:\\aaa\\students.txt"));) {
            Student student = null;
            String str = null;
            while ((str = br.readLine()) != null) {
                maxSid = str.split("-")[0];
            }
            return maxSid;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Student> selectAll() {
        try (BufferedReader br = new BufferedReader(
                new FileReader("d:\\aaa\\students.txt"));) {
            List<Student> students = new ArrayList<Student>();
            Student student = null;
            String str = null;
            while ((str = br.readLine()) != null) {
                String[] strs = str.split("-");
                student = new Student();
                student.setSid(strs[0]);
                student.setSname(strs[1]);
                student.setPwd(strs[2]);
                student.setAge(Integer.parseInt(strs[3]));
                student.setEmail(strs[4]);
                students.add(student);
            }
            return students;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


}
